﻿using Microsoft.WindowsAPICodePack.Dialogs;
using System;
using System.Diagnostics;
using System.IO;
using System.Windows;

namespace WpfApp1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
        {
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var test = Convert.ToInt16("B2", 16);//196
            var test1 = Convert.ToInt16("6B", 16);//227
            var text0 = Convert.ToInt16("89", 16) ^ test;//59
            var text11 = Convert.ToInt16("50", 16) ^ test1;//59

            var path = "";
            using (var folderBrowser = new CommonOpenFileDialog())
            {
                folderBrowser.IsFolderPicker = true;
                folderBrowser.Multiselect = false;
                folderBrowser.Title = "选择考生照片所在文件夹";
                if (folderBrowser.ShowDialog(GetWindow(this)) != CommonFileDialogResult.Ok) return;
                path = folderBrowser.FileName;
                if (!Directory.Exists(path))
                {
                    MessageBox.Show(@"所选路径不存在或无权访问", @"错误", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
            }
            //jpg文件头16进制为 FFD8FFE0
            //png文件头16进制为 89504E47
            //gif文件头16进制为 47494638
            //其它文件类型头部信息：https://www.cnblogs.com/qfcndtt/archive/2012/05/08/2489761.html

            var jpgHeader = Convert.ToInt16("FFD8", 16);
            var pngHeader = Convert.ToInt16("8950", 16);
            var gifHeader = Convert.ToInt16("4749", 16);

            foreach (var file in Directory.GetFiles(path, "*.dat", SearchOption.TopDirectoryOnly))
            {
                var fileInfo = new FileInfo(Path.Combine(path, file));
                var topBytes = new byte[2];
                var allBytes = new byte[fileInfo.Length];

                var imgXorHeader = 0;
                using (var sr = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    sr.Read(allBytes, 0, allBytes.Length);
                }

                Array.Copy(allBytes, topBytes, 2);

                var strHex = BitConverter.ToString(topBytes).Replace("-", "");//文件头部2个字节的16进制
                var fileHeader1 = Convert.ToInt16(strHex, 16);//头部字节转int16
                var newFile = "";
                var going = true;

                if ((topBytes[0] ^ Convert.ToInt16("FF", 16)) == (topBytes[1] ^ Convert.ToInt16("D8", 16)))
                {
                    imgXorHeader = jpgHeader ^ fileHeader1;//和固定的图片头部信息的2个字节的int16做xor
                    newFile = $"{fileInfo.Name.Substring(0, fileInfo.Name.LastIndexOf('.'))}.jpg";
                }
                else if ((topBytes[0] ^ Convert.ToInt16("89", 16)) == (topBytes[1] ^ Convert.ToInt16("50", 16)))
                {
                    imgXorHeader = pngHeader ^ fileHeader1;
                    newFile = $"{fileInfo.Name.Substring(0, fileInfo.Name.LastIndexOf('.'))}.png";
                }
                else if ((topBytes[0] ^ Convert.ToInt16("47", 16)) == (topBytes[1] ^ Convert.ToInt16("49", 16)))
                {
                    imgXorHeader = gifHeader ^ fileHeader1;
                    newFile = $"{fileInfo.Name.Substring(0, fileInfo.Name.LastIndexOf('.'))}.gif";
                }
                else
                {
                    Debug.WriteLine($"未知的文件格式{fileInfo.FullName}");
                    going = false;
                }

                if (!going) return;
                var xorHeader = Convert.ToString(imgXorHeader, 16);//xor后的16进制表现形式

                using (var sw = new FileStream(Path.Combine(path, newFile), FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
                {
                    foreach (var b in allBytes)
                    {
                        sw.Write(BitConverter.GetBytes(b ^ imgXorHeader), 0, 1);
                    }
                    sw.Flush();
                }
            }

            MessageBox.Show("完成");
        }
    }
}